package com.hellobike.hitch.business.main.view

import android.content.Context
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v7.app.AlertDialog
import android.view.View

/**
 * 队列管理的Dialog
 * created by chenqiuyue on 2019/1/2 16:21
 */
open class QueueDialog(var context: Context, @LayoutRes resID: Int) {
    private var dialog: AlertDialog
    private var disMissListener: DialogDisMissListener? = null

    init {
        val view = View.inflate(context, resID, null)
        dialog = AlertDialog.Builder(context).run {
            setView(view)
            setCancelable(true)
            create()
        }
    }

    fun setViewClickListener(@IdRes viewId: Int, l: View.OnClickListener): QueueDialog {
        dialog.window.decorView.findViewById<View>(viewId).setOnClickListener(l)
        return this
    }

    fun show() {
        if (dialog != null && !dialog.isShowing) {
            dialog.show()
            dialog.setOnDismissListener { disMissListener?.onDissMiss() }
            return
        }
    }

    fun dismiss() {
        dialog.dismiss()
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss()
        }
    }


    fun setDissMissListener(listener: DialogDisMissListener?) {
        disMissListener = listener
    }

        interface DialogDisMissListener {
        fun onDissMiss()
    }
}

